﻿///***************************************************************
/// 시  스  템 : UPDMS.Updms.Usr
/// 단위시스템 : USR
/// 계      층 : Presentation Layer
/// 프로그램명 : Usr1011m
/// 파  일  명 : Usr1011m.aspx.cs
/// 설      명 : 즐겨찾기
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2014-08-25 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;
using Updms.Usr.Exe;

namespace Updms.Usr
{
	public partial class Usr1011m : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		public static readonly string PID = "Usr1011m";
		public static readonly string gs_path_info = "/updms/Usr/" + PID + ".aspx";

		Global global = new Global();

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				OnPageLoad();
				SetInit();
				SelectProcess();
			}
		}

		//OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
		}

		#endregion

		#region SetInit

		//SetInit
		private void SetInit()
		{
			if (Request.QueryString["pgm_id"] != null)
			{
				h_pgm_id.Text = Server.UrlDecode(Request.QueryString["pgm_id"].ToString().Trim());
			}
			if (Request.QueryString["search_condition"] != null)
			{
				h_search_condition.Text = Server.UrlDecode(Request.QueryString["search_condition"].ToString().Trim());
			}
		}

		#endregion

		#region 디테일 Method

		//즐겨찾기 변경
		protected void ddl_favorite_list_SelectedIndexChanged(object sender, EventArgs e)
		{
			//제목
			string ls_title = "";
			if (ddl_favorite_list.SelectedIndex > 0)
			{
				ls_title = ddl_favorite_list.SelectedItem.Text;
			}

			txt_title.Text = ls_title;

			//Url
			string ls_favorite_value = ddl_favorite_list.SelectedValue;
			string ls_url = "";

			if (ls_favorite_value.Contains("|"))
			{
				string[] la_favorite_value = ls_favorite_value.Split('|');
				if (la_favorite_value.Length > 0)
				{
					ls_url = la_favorite_value[1];
				}
			}

			ibtn_go_url.Attributes.Remove("onclick");
			if (!StringUtil.IsNullOrEmpty(ls_url))
			{
				ibtn_go_url.Attributes.Add("onclick", "moveParentUrl('" + ls_url + "', 'normal');");
			}
		}

		#endregion

		#region CRUD (SelectProcess, InserProcess, UpdateProcess, DeleteProcess)

		//즐겨찾기 조회
		private void SelectProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_user_id", typeof(string), USER_ID);
			lo_dp.AddProperty("ls_pgm_id", typeof(string), h_pgm_id.Text);

			using (Usr1011m_NT _Usr1011m_NT = new Usr1011m_NT())
			{
				lo_ds.Merge(_Usr1011m_NT.SelectProcess(lo_dp));
			}

			ddl_favorite_list.Items.Clear();
			ddl_favorite_list.DataSource = lo_ds;
			ddl_favorite_list.DataMember = "UPDMS_USR_FAVORITE_SEARCH";
			ddl_favorite_list.DataTextField = "Title";
			ddl_favorite_list.DataValueField = "Seq_Url";
			ddl_favorite_list.DataBind();
			ddl_favorite_list.Items.Insert(0, new ListItem(DDL_SELECT, ""));
		}

		//저장
		private void InsertProcess(DataPack dp)
		{
			try
			{
				using (Usr1011m_RT _Usr1011m_RT = new Usr1011m_RT())
				{
					int li_return = _Usr1011m_RT.InsertProcess(dp);
					string ls_msg_id = (li_return > 0) ? "MSG_INF_007" : "MSG_ERR_105";	//저장되었습니다:저장할 수 없습니다
					string ls_msg_tp = (li_return > 0) ? "IC" : "AC";

					CallMsgBoxInAjax(ls_msg_id, null, ls_msg_tp);
				}
			}
			catch (Exception ex)
			{
				CallExceptionMsgBoxInAjax(ex);
			}
		}

		//수정
		private void UpdateProcess(DataPack dp)
		{
			try
			{
				using (Usr1011m_RT _Usr1011m_RT = new Usr1011m_RT())
				{
					int li_return = _Usr1011m_RT.UpdateProcess(dp);
					string ls_msg_id = (li_return > 0) ? "MSG_INF_006" : "MSG_ERR_104";	//수정되었습니다:수정할 수 없습니다
					string ls_msg_tp = (li_return > 0) ? "IC" : "AC";

					CallMsgBoxInAjax(ls_msg_id, null, ls_msg_tp);
				}
			}
			catch (Exception ex)
			{
				CallExceptionMsgBoxInAjax(ex);
			}
		}

		//삭제
		private void DeleteProcess()
		{
			string ls_seq = ddl_favorite_list.SelectedValue;
			ls_seq = ls_seq.Substring(0, ls_seq.IndexOf("|"));
			string ls_xml = "<UPDMS><UPDMS_USR_FAVORITE_SEARCH><Seq>" + ls_seq + "</Seq></UPDMS_USR_FAVORITE_SEARCH></UPDMS>";
			
			try
			{
				using (Usr1011m_RT _Usr1011m_RT = new Usr1011m_RT())
				{
					int li_return = _Usr1011m_RT.DeleteProcess(ls_xml);
					string ls_msg_id = (li_return > 0) ? "MSG_INF_004" : "MSG_ERR_103";	//삭제되었습니다:삭제할 수 없습니다
					string ls_msg_tp = (li_return > 0) ? "IC" : "AC";

					CallMsgBoxInAjax(ls_msg_id, null, ls_msg_tp);
				}
			}
			catch (Exception ex)
			{
				CallExceptionMsgBoxInAjax(ex);
			}
		}

		#endregion

		#region SetValue

		//SetValue
		private DataPack SetValue()
		{
			DataPack lo_dp = new DataPack();

			//Seq
			string ls_favorite_value = ddl_favorite_list.SelectedValue;
			string ls_seq = "";

			if (ls_favorite_value.Contains("|"))
			{
				string[] la_favorite_value = ls_favorite_value.Split('|');
				if (la_favorite_value.Length > 0)
				{
					ls_seq = la_favorite_value[0];
				}
			}

			//sm, page 처리
			string ls_search_condition = h_search_condition.Text;
			int li_start = ls_search_condition.IndexOf("&");
			ls_search_condition = ls_search_condition.Substring(li_start, ls_search_condition.Length - li_start);
			ls_search_condition = ls_search_condition.Replace("&page=", "");

			lo_dp.AddProperty("Seq", typeof(string), ls_seq);
			lo_dp.AddProperty("User_Id", typeof(string), USER_ID);
			lo_dp.AddProperty("Pgm_Id", typeof(string), h_pgm_id.Text);
			lo_dp.AddProperty("Title", typeof(string), txt_title.Text.Trim());
			lo_dp.AddProperty("Search_Condition", typeof(string), ls_search_condition);
			lo_dp.AddProperty("Create_User", typeof(string), USER_ID);
			lo_dp.AddProperty("Create_Dt", typeof(string), "");
			lo_dp.AddProperty("Update_User", typeof(string), USER_ID);
			lo_dp.AddProperty("Update_Dt", typeof(string), "");

			return lo_dp;
		}

		#endregion

		#region Page_PreRender

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				try
				{
					using (AccessCheck access = new AccessCheck())
					{
						//접근권한 체크
						if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
						{
							Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
						}
						//사용권한별 버튼처리
						PagePreRender.SetVisibleCtrl(this, access.CheckPgmUsingGrant(USER_DIV, PID), access.GetVisibleCtrl(PID));
					}
				}
				catch (Exception ex)
				{
					CallExceptionMsgBoxInAjax(ex);
				}
			}
		}

		#endregion

		#region 버튼이벤트

		//저장
		protected void ibtn_save_Click(object sender, EventArgs e)
		{
			if (ddl_favorite_list.SelectedIndex == 0)
			{
				InsertProcess(SetValue()); 
			}
			else
			{
				UpdateProcess(SetValue());
			}
		}

		//삭제
		protected void ibtn_delete_Click(object sender, EventArgs e)
		{
			DeleteProcess();
		}

		#endregion
	}
}